//
//  AnswerDetailTwoController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/20.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "AnswerDetailTwoController.h"
#import "UserIconAndNameView.h"
#import "AnswerDetailCommentsCell.h"
#import "AnswerDetailTwoModel.h"

@interface AnswerDetailTwoController ()<UITableViewDelegate,UITableViewDataSource>
/**底部输入框视图*/
@property (nonatomic,strong)CommentsBottomView*bottomView;
/**键盘上面评论输入框*/
@property (nonatomic,strong)PubulicInputToolbar*inputToolbar;

/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;

/**导航栏文字*/
@property (nonatomic,strong)UIView*navTitleView;
@end

@implementation AnswerDetailTwoController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type == AnswerDetailMe){
        self.title = @"我的回答";
        [self addNavigationItemWithImageNames:@[@"sangedian"] isLeft:NO target:self action:@selector(rightAction) tags:nil];
    }else{
        self.title = @"答题详情";
        [self setNavTitle];
        self.navTitleView = [self createTitleView];
    }
    [self createUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setIQKeyboardManagerEnable:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setIQKeyboardManagerEnable:YES];
}
-(void)rightAction{
    PresentationObject * object = [PresentationObject sharedPresentationObject];
    object.subViewFrame = CGRectZero;
    [object showPresentingViewController:self PresentedViewController:[ShareViewController new]];

}

/// 写回答
-(void)writeAnswerAction{
    AskQuestionsController * askVC = [AskQuestionsController new];
    askVC.type = AskQuestionsAnswer;
    [self.navigationController pushViewController:askVC animated:YES];
}

-(void)createUI{
    
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.listView];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.inputToolbar];

}
-(void)setNavTitle{
    UIButton * oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [oneBtn setImage:IMAGE_NAMED(@"xiepinglun") forState:UIControlStateNormal];
    [oneBtn setTitle:@"写回答" forState:UIControlStateNormal];
    [oneBtn setTitleColor:UIColorFromRGB(0x454F44) forState:UIControlStateNormal];
    oneBtn.titleLabel.font = SYSTEMFONT(14);
    [oneBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 4, 0, -4)];
    
    kWeakSelf(weakSelf);
    [oneBtn addActionHandler:^{
        [weakSelf writeAnswerAction];
    }];
    
    UIButton * twoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [twoBtn setImage:IMAGE_NAMED(@"sangedian") forState:UIControlStateNormal];
    
    [twoBtn addActionHandler:^{
        [weakSelf rightAction];
    }];
    
    UIBarButtonItem * oneItem = [[UIBarButtonItem alloc] initWithCustomView:oneBtn];
    UIBarButtonItem * twoItem = [[UIBarButtonItem alloc] initWithCustomView:twoBtn];
    self.navigationItem.rightBarButtonItems = @[twoItem,oneItem];

}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    DLogFunc;
    [self.view endEditing:YES];
}

#pragma mark ---UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    AnswerDetailTwoModel * model = self.dataArray[section];
    
    if (model.modelArray.count > 0) {
        return model.modelArray.count;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AnswerDetailCommentsTitleCell * oneCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AnswerDetailCommentsTitleCell class])];
    AnswerDetailCommentsContexCell * twoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AnswerDetailCommentsContexCell class])];
    //带有二级评论的cell
    AnswerDetailCommentsCell * thereCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AnswerDetailCommentsCell class])];
    //只有一级评论的cell
    AnswerDetailCommentsOneCell * fourCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AnswerDetailCommentsOneCell class])];
    
    
    AnswerDetailTwoModel * model = self.dataArray[indexPath.section];

    if (indexPath.section == 0) {
        oneCell.ansTwoModel = model;
        return oneCell;
    }
    if (indexPath.section == 1) {
        twoCell.type = self.type;
        twoCell.ansTwoModel = model;
        return twoCell;
    }
    if (model.modelArray.count > 0) {
        UserCommentsModel * commenModel = model.modelArray[indexPath.row];
        if (commenModel.isTwoComments) {
            thereCell.commenModel = commenModel;
            return thereCell;
        }else{
            fourCell.commenModel = commenModel;
            return fourCell;
        }
    }
//
//    if (indexPath.section == 2 && indexPath.row == 0) {
//        thereCell.authorLb.hidden = YES;
//        return thereCell;
//    }
    return fourCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.type == AnswerDetailMe) {
        return;
    }
    NSIndexPath * index = [NSIndexPath indexPathForRow:0 inSection:0];
    //标记的cell  在tableView中的坐标值
    CGRect  recttIntableview = [self.listView rectForRowAtIndexPath:index];
    //当前cell在屏幕中的坐标值
    CGRect rectInSuperView = [self.listView convertRect:recttIntableview toView:[self.listView superview]];
    
    DLog(@"滑动到%f",rectInSuperView.origin.y);
    
    if (rectInSuperView.origin.y < -K_TopHeight) {
        self.navigationItem.titleView = self.navTitleView;
    }else{
        self.navigationItem.titleView =nil;
        self.title = @"答题详情";
    }
    
}

-(PubulicInputToolbar*)inputToolbar{
    if(!_inputToolbar){
        _inputToolbar=[[PubulicInputToolbar alloc] init];
        _inputToolbar.textViewMaxLine = 3;
        _inputToolbar.fontSize = 18;
        _inputToolbar.placeholder = @"写评论...";
        kWeakSelf(weakSelf);
        [_inputToolbar inputToolbarSendText:^(NSString * _Nonnull text) {
            // 清空输入框文字
            [weakSelf.inputToolbar bounceToolbar];
        }];
    }
    return _inputToolbar;
}
-(CommentsBottomView*)bottomView{
    if(!_bottomView){
        _bottomView=[[CommentsBottomView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        [_bottomView settextFieldEnabled:^{
            [weakSelf.inputToolbar popToolbar];
        }];
    }
    return _bottomView;
}
-(UIView *)createHeaderView{
    UIView * view = UIView.new;
    
    UILabel * titelLabel = UILabel.new;
    titelLabel.textColor = UIColorFromRGB(0x273323);
    titelLabel.font = BOLDSYSTEMFONT(18);
    titelLabel.text = @"西红柿炒蛋先放西红柿还是先放鸡蛋？";
    titelLabel.numberOfLines = 0;
    
    UILabel * numberLabel = UILabel.new;
    numberLabel.textColor = UIColorFromRGB(0x959E97);
    numberLabel.font = BOLDSYSTEMFONT(12);
    numberLabel.text = @"12个回答";
    
    UIImageView * image = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"more")];
    
    [view addSubview:titelLabel];
    [view addSubview:numberLabel];
    [view addSubview:image];
    
    [titelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
    }];
    
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(titelLabel.mas_bottom).offset(8);
        make.bottom.mas_equalTo(-16);
    }];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(numberLabel.mas_right).offset(1);
        make.centerY.mas_equalTo(numberLabel);
    }];


    return view;
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[AnswerDetailCommentsTitleCell class] forCellReuseIdentifier:NSStringFromClass([AnswerDetailCommentsTitleCell class])];
        [_listView registerClass:[AnswerDetailCommentsContexCell class] forCellReuseIdentifier:NSStringFromClass([AnswerDetailCommentsContexCell class])];
        [_listView registerClass:[AnswerDetailCommentsCell class] forCellReuseIdentifier:NSStringFromClass([AnswerDetailCommentsCell class])];
        [_listView registerClass:[AnswerDetailCommentsOneCell class] forCellReuseIdentifier:NSStringFromClass([AnswerDetailCommentsOneCell class])];

    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc]init];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"AnswerDetailTwo" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            AnswerDetailTwoModel *model = [AnswerDetailTwoModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataArray addObject:model];
        }];
    }
    return _dataArray;
}

-(UIView *)createTitleView{
    UIView * view = UIView.new;
    
    UILabel * topLabel = UILabel.new;
    topLabel.textColor = UIColorFromRGB(0x454F44);
    topLabel.font = SYSTEMFONT(14);
    topLabel.text = @"西红柿炒蛋先放西红柿还是…放鸡蛋？";
    
    UILabel * bomLabel = UILabel.new;
    bomLabel.textColor = UIColorFromRGB(0x454F44);
    bomLabel.font = SYSTEMFONT(14);
    bomLabel.text = @"12个回答";
    
    UIImageView * image = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"more")];
    
    
    [view addSubview:topLabel];
    [view addSubview:bomLabel];
    [view addSubview:image];
    
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [bomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.top.mas_equalTo(topLabel.mas_bottom).offset(2);
    }];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(bomLabel.mas_right).offset(1);
        make.centerY.mas_equalTo(bomLabel);
    }];
    
    return view;
}
@end
